#ifndef __MOTOR_H_
#define __MOTOR_H_

#include <Arduino.h>

#define DIR_OVER     3u
#define DIR_UNLOAD   2u
#define DIR_LOAD     1u
#define DIR_STOP     0u

typedef struct {
  uint8_t pinP :4;             //IO+
  uint8_t pinN :4;             //IO-
  uint8_t direction     : 2;   //旋转方向
  uint8_t directionLast : 2;   //旋转方向
  uint8_t overtick      : 4;   //过流计次
  uint8_t runtick;             //计时器 50ms
  uint16_t stuckThreshold;     //卡料时ADC值
  uint16_t stuckAdcValue;      //实时ADC数值

}MotorCHxCfg;

typedef struct {
  void (*init)(void);
  void (*loop)(void);
  void (*keepLow)(void);
  void (*hander)(MotorCHxCfg* mt);      //电机运行状态机处理函数

  void (*run_ms)(MotorCHxCfg* mt,uint8_t dir,uint32_t ms); //运行 DIR_STOP:延迟后暂停, DIR_LOAD:进料后再延迟后暂停, DIR_UNLOAD :退料再延迟后暂停
  void (*load)(MotorCHxCfg* mt);      //进料
  void (*unload)(MotorCHxCfg* mt);    //退料
  void (*stop)(MotorCHxCfg* mt);      //释放
  void (*stuck_value_set)(MotorCHxCfg* mt, uint16_t value);    //到位后ADC设置函数
  boolean (*stuck_state_get) (MotorCHxCfg* mt);   //是否卡料 1:卡料; 0:未卡料
}MotorCHxOPS;

typedef struct {
  MotorCHxCfg *mt[4];
  MotorCHxOPS* ops;
}MOTOR;

extern MOTOR motor;

#endif //__MOTOR_H_